/**
 * Created by ranYun on 2017/6/8.
 * tab切换,辅助添加class,每次点击生成回调(点击元素,点击第几个)
 * new ToggleCheck(array:elements,string:activeClass,number:defaultActiveOrder,function:callback);
 */
function ToggleCheck(eles,activeClass,initNum,callBack) {
    if(!eles || !eles.length || !(eles instanceof Array)){
        throw new Error("no elements for init");
    }
    this.elements = [];
    this.activeClass = activeClass;
    this.callBack = false || callBack;
    for(var x=0;x<eles.length;x++){
        var ele = document.querySelector(eles[x]);
        this.elements.push(ele);
        ele.onclick = function (x) {
            this.doClick(x);
        }.bind(this,x)
    }
    this.doClick(initNum);
}
ToggleCheck.prototype = {
    doClick:function (index) {
        for(var x=0;x<this.elements.length;x++){
            var cs = this.elements[x].className;
            this.elements[x].className = cs.replace(" "+this.activeClass,"");
            if(x == index){
                this.elements[x].className = this.elements[x].className+" "+this.activeClass;
                this.callBack&&this.callBack(this.elements[x],x);
            }
        }
    }
};